﻿using LCL.LogNet;
using LCL.Network;
using LCL.Network.Enthernet;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace LCL.SimplifyNetServer
{
    public partial class FormServer : Form
    {
        public FormServer()
        {
            InitializeComponent();
        }
        private void FormServer_Load(object sender, EventArgs e)
        {
            textBox3.Text = Guid.Empty.ToString();
            if (simplifyServer == null)
            {
                this.butDisconnect.Enabled = true;
            }
            else
            {
                this.butDisconnect.Enabled = false;
            }
        }
        private void butDisconnect_Click(object sender, EventArgs e)
        {
            this.butConnect.Enabled = true;
            this.butDisconnect.Enabled = false;
            if (simplifyServer != null || simplifyServer.IsStarted)
            {
                simplifyServer.ServerClose();
            }
            simplifyServer = null;
            timerSecond = null;
        }
        private void butConnect_Click(object sender, EventArgs e)
        {
            this.butConnect.Enabled = false;
            this.butDisconnect.Enabled = true;
            Start();
        }
        private NetSimplifyServer simplifyServer;
        private Timer timerSecond;
        private void Start()
        {
            try
            {
                simplifyServer = new NetSimplifyServer();                                          // 实例化
                simplifyServer.Token = new Guid(textBox3.Text);                                   // 设置令牌
                simplifyServer.ReceiveStringEvent += SimplifyServer_ReceiveStringEvent; // 接收字符串的时候触发
                simplifyServer.LogNet.BeforeSaveToFile += LogNet_BeforeSaveToFile;                  // 日志保存前先显示出来
                simplifyServer.ServerStart(int.Parse(textBox1.Text));                           // 启动服务

                timerSecond = new Timer();// 这个定时器的功能是每秒更新在线的客户端数量
                timerSecond.Interval = 1000;
                timerSecond.Tick += TimerSecond_Tick;
                timerSecond.Start();
            }
            catch (Exception ex)
            {
                MessageBox.Show("创建失败：" + ex.Message);
                this.butConnect.Enabled = true;
                this.butDisconnect.Enabled = false;
            }
        }
        private void TimerSecond_Tick(object sender, EventArgs e)
        {
            if (simplifyServer != null)
            {
                label6.Text = simplifyServer.ClientCount.ToString();
            }
        }
        private void SimplifyServer_ReceiveStringEvent(AppSession session, NetHandle handle, string value)
        {
            if (handle == 1)
            {
                // 当收到客户端发来的信号1的时候进行操作的消息
                simplifyServer.SendMessage(session, handle, "这是测试信号：" + value);
            }
            else if (handle < 100)
            {
                simplifyServer.SendMessage(session, handle, "这是测试信号：" + handle);
            }
            else
            {
                simplifyServer.SendMessage(session, handle, "不支持的消息");
            }
            // 显示出来，谁发的，发了什么
            textBox2.Invoke(new Action(() =>
            {
                textBox2.AppendText("" + session + " [" + handle + "] " + value + "" + Environment.NewLine);
            }));

        }
        private void LogNet_BeforeSaveToFile(object sender, LogEventArgs e)
        {
            if (InvokeRequired)
            {
                BeginInvoke(new Action<object, LogEventArgs>(LogNet_BeforeSaveToFile), sender, e);
                return;
            }
            textBox2.AppendText(e.HslMessage.ToString() + Environment.NewLine);
        }
        private void myIcon_MouseClick(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                myMenu.Show();
            }

            if (e.Button == MouseButtons.Left)
            {
                this.Visible = true;
                this.WindowState = FormWindowState.Normal;
            }
        }
        private void myIcon_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            //判断是否已经最小化于托盘 
            if (WindowState == FormWindowState.Minimized)
            {
                //还原窗体显示 
                WindowState = FormWindowState.Normal;
                //激活窗体并给予它焦点 
                this.Activate();
                //任务栏区显示图标 
                this.ShowInTaskbar = true;
                //托盘区图标隐藏 
                myIcon.Visible = false;
            }
        }
        private void 显示窗口ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (this.Visible == false) this.Visible = true;//假如当前窗口没显示则显示当前窗口 
        }
        private void 隐藏窗口ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (this.Visible == true) this.Visible = false;//假如当前窗口为显示的则隐藏窗口 
        }
        private void 退出程序ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            simplifyServer = null;
            this.Close();//关闭当前对象(即窗体) 
            Application.Exit();//通过Application类的静态方法Exit()退出应用程序 
        }
        private void FormServer_SizeChanged(object sender, EventArgs e)
        {
            //判断是否选择的是最小化按钮 
            if (WindowState == FormWindowState.Minimized)
            {
                //隐藏任务栏区图标 
                this.ShowInTaskbar = false;
                //图标显示在托盘区 
                myIcon.Visible = true;
            }
        }
    }
}
